package gamble.money.command;


import java.io.IOException;

import gamble.money.dao.MoneyDoaImpl;
import gamble.money.email.CreateRandomNum;
import gamble.money.email.SendMail;
import gamble.money.email.SendMailImp;
import gamble.money.vo.MoneyVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MoneyController {
	//메인페이지
	@RequestMapping("/chargemain.do")
	public String chargemain(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.setAttribute("id", "park");
		return "money/chargemain";
	}
	
	//충전 페이지
	@RequestMapping("/chargemoney.do")
	public String charge(){
		
		return "money/chargemoney";
	}
//	//출금 페이지
//	@RequestMapping("/withdrawalmoney.do")
//	public String withdrawalmoney(){
//		return "money/withdrawalmoney";
//	}
	
	//메일 보내기
	@RequestMapping("/sendmail.do")
	public ModelAndView sendMail(HttpServletRequest request) throws IOException{
		String email = request.getParameter("email");
//		System.out.println("email : " + email);
		
		//비지니스로직 Start (빼야하지만 일단 박아둠)
		String ranNo  = new CreateRandomNum().createNumber();
		System.out.println("생성된 랜덤 번호 : "+ ranNo);
		
		HttpSession session = request.getSession();
		session.setAttribute("ranNo", ranNo);
//		session.setAttribute("id", "park");
		
		SendMail mSendMail = new SendMailImp(email, ranNo);
		boolean resultBoolean = mSendMail.sendmailtoCliend();
		//----------------------------------------
		
		//test
		String result ="fail";
		result = (resultBoolean== true)? "success" : "fail";
		System.out.println("result : "+ result);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("result",result);
		mv.setViewName("money/sendMail");
		return mv;
	}
}
